package com.yangle.frame.admin.service;

import com.yangle.frame.admin.entity.FrameDatabase;
import com.yangle.frame.admin.entity.FrameTable;
import com.yangle.frame.admin.entity.FrameMenu;
import com.yangle.frame.admin.mapper.FrameDatabaseMapper;
import com.yangle.frame.admin.mapper.FrameTableMapper;
import com.yangle.frame.admin.mapper.FrameMenuMapper;
import com.yangle.frame.common.service.BaseService;
import com.yangle.frame.common.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
* Created by yangle on 2018/5/27.
*/
@Service
public class FrameTableService implements BaseService<FrameTable> {
@Autowired
private FrameTableMapper FrameTableMapper;
@Autowired
private FrameDatabaseMapper frameDatabaseMapper;
    @Value("${com.yangle.database}")
    public String databasename;
public List<FrameTable> getAll(FrameTable FrameTable) {
Example example = MapperUtils.getExampleByClass(FrameTable.class, FrameTable);
return FrameTableMapper.selectByExample(example);
}
public void createTable(String tableName){

}

    /**
     * 删除字段
     * @param table
     */
    public void dropField(String ids){
        for(String id:ids.split(",")){
            FrameTable table = FrameTableMapper.selectByPrimaryKey(id);
            FrameDatabase db = frameDatabaseMapper.selectByPrimaryKey(table.getTableId());
            FrameTableMapper.dropField(db.getTableSqlName(),table.getFieldName());}

    }
    public void createOrAlterField(FrameTable table){
    String fieldType=table.getFieldType();
        //对非时间字段进行长度拼接
        if(!table.getFieldType().equals("datetime")){
            fieldType=table.getFieldType()+"("+table.getFieldLength()+")";
        }
        //获取名
        FrameDatabase database = frameDatabaseMapper.selectByPrimaryKey(table.getTableId());

        //判断id是否为空，如果为空则进行字段的修改
        if(StringUtil.isNotEmpty(table.getId())){//不为空则说明是修改字段的操作
            //获取原有表的名称
            FrameTable preField = FrameTableMapper.selectByPrimaryKey(table.getId());

            String preFieldName = preField.getFieldName();
            String fieldName=table.getFieldName();
//            if(!preFieldName.equals(fieldName)){
                //执行字段的修改操作
                //①查询字段是否存在
                int i = FrameTableMapper.queryFieldIsExists(databasename,database.getTableSqlName(), preFieldName);
                if(i>0){//说明存在，执行表名的修改操作
                    FrameTableMapper.alterFieldAttr(preFieldName,database.getTableSqlName(),fieldName,fieldType,table.getFieldMark());
                }else{
                    FrameTableMapper.addField(database.getTableSqlName(),fieldName,fieldType,table.getFieldMark());
                }
//            }
        }else{//新创建
            //①查询表是否存在
            int i = FrameTableMapper.queryFieldIsExists(databasename,database.getTableSqlName(), table.getFieldName());
            if(i==0){//不存在才创建
                FrameTableMapper.addField(database.getTableSqlName(),table.getFieldName(),fieldType,table.getFieldMark());
            }
        }
    }
public int queryTableIsExists(String tableName){
    return FrameTableMapper.queryTableIsExists(databasename,tableName);
}
public int queryFieldIsExists(String tableName, String fieldName){
    return FrameTableMapper.queryFieldIsExists(databasename,tableName,fieldName);
}
public void create(FrameTable FrameTable) {
FrameTableMapper.insert(FrameTable);
}

public void update(FrameTable FrameTable) {
FrameTableMapper.updateByPrimaryKey(FrameTable);
}

public FrameTable select(String id) {
return FrameTableMapper.selectByPrimaryKey(id);
}

public void delete(String ids) {
FrameTableMapper.deletes(Arrays.asList(ids.split(",")));
}
}
